﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.HttpContext

<Serializable(), ParseChildren(False)> _
Public Class ColecaoControles
    Inherits System.Collections.CollectionBase


    Public Function ScriptLinkBusca()

        Dim strJSCript As String = "javascript:"



        For Each Ctrl As ItemControle In Me.InnerList
            If Not IsDBNull(Ctrl.Valor) Then
                Dim strValor As String = Ctrl.Valor
                If Ctrl.Formato.Trim = String.Empty Then
                    strJSCript &= "window.opener.document.forms[0]." & Ctrl.ClientId & ".value='" & strValor.Replace("'", "").Replace("&nbsp;", "") & "';"
                Else
                    If IsNumeric(Ctrl.Valor) Then
                        strJSCript &= "window.opener.document.forms[0]." & Ctrl.ClientId & ".value='" & CDbl(strValor).ToString(Ctrl.Formato, System.Globalization.CultureInfo.CurrentCulture) & "';"
                    ElseIf IsDate(Ctrl.Valor) Then
                        strJSCript &= "window.opener.document.forms[0]." & Ctrl.ClientId & ".value='" & CDate(strValor).ToString(Ctrl.Formato, System.Globalization.CultureInfo.CurrentCulture) & "';"
                    Else
                        strJSCript &= "window.opener.document.forms[0]." & Ctrl.ClientId & ".value='" & strValor.Replace("'", "").Replace("&nbsp;", "") & "';"
                    End If
                End If
            End If
        Next
        strJSCript &= "window.close();"

        Return strJSCript
    End Function

    Public Function ScriptLimpezaControles()

        Dim strJSCript As String = "javascript:"

        For Each Ctrl As ItemControle In Me.InnerList
            If Not Ctrl.ControleBaseBusca And Ctrl.ClientId.Trim <> String.Empty Then
                strJSCript &= "window.document.forms[0]." & Ctrl.ClientId & ".value='';"
            End If
        Next

        Return strJSCript
    End Function

    Public Sub CarregarObjetosDestinos()

        Dim arrObjetosDestinos() As String

        If Not Current.Request.QueryString("objetos_destinos") Is Nothing Then

            arrObjetosDestinos = Current.Request.QueryString("objetos_destinos").Split(",")

            For Each strControleDestino As String In arrObjetosDestinos

                Dim C As New Componentes.ItemControle

                C.ClientId = strControleDestino.Split("|")(0)
                C.NomeColunaResultado = strControleDestino.Split("|")(1)
                Me.Add(C)

            Next

        Else

            Dim strIdObjetoDestino As String = ""
            Dim strIdObjetoDescricao As String = ""

            If Not Current.Request.QueryString("objeto_destino") Is Nothing Then
                strIdObjetoDestino = Current.Request.QueryString("objeto_destino")
            Else
                strIdObjetoDestino = ""
            End If

            If Not Current.Request.QueryString("objeto_descricao") Is Nothing Then
                strIdObjetoDescricao = Current.Request.QueryString("objeto_descricao")
            Else
                strIdObjetoDescricao = ""
            End If

            If strIdObjetoDestino.Trim <> String.Empty And _
               strIdObjetoDescricao.Trim <> String.Empty Then

                Dim C1 As New Componentes.ItemControle

                C1.ClientId = strIdObjetoDestino
                C1.NomeColunaResultado = ""
                Me.Add(C1)

                Dim C2 As New Componentes.ItemControle

                C2.ClientId = strIdObjetoDescricao
                C2.NomeColunaResultado = 1
                Me.Add(C2)


            End If

        End If

    End Sub


    Public Function ControleBaseBusca() As ItemControle

        For Each IC As ItemControle In Me.InnerList

            If IC.ControleBaseBusca Then

                Return IC

            End If

        Next

        Return Nothing

    End Function

    Public Shared Function ObterControleWeb(ByRef ctlOrigem As Control, ByRef strNomeControle As String) As Control

        Dim cControle As New Control

        Dim terminar As Boolean = False
        Dim ctlParent As Control = ctlOrigem.Parent

        Do While Not terminar

            If ctlParent Is Nothing Then

                terminar = True

            Else

                cControle = ctlParent.FindControl(strNomeControle)

            End If

            If Not cControle Is Nothing Then
                terminar = True
            End If

            If Not ctlParent.Parent Is Nothing Then
                ctlParent = ctlParent.Parent
            Else
                terminar = True
            End If
        Loop

        Return cControle

    End Function

    Public Function ObterControlePorChave(ByVal strChave As String) As ItemControle

        For Each IC As ItemControle In Me.InnerList

            If IC.Chave.Trim.ToUpper(System.Globalization.CultureInfo.CurrentCulture) = strChave.Trim.ToUpper(System.Globalization.CultureInfo.CurrentCulture) Then

                Return IC

            End If

        Next

        Return Nothing

    End Function

    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
         Editor(GetType(ColecaoControlesEditor), GetType(System.Drawing.Design.UITypeEditor)), _
         PersistenceMode(PersistenceMode.InnerProperty)> _
       Public Function Item(ByVal i As Long) As ItemControle

        Return Me.InnerList(i)

    End Function

    Public Sub Add(ByVal Ctrl As ItemControle)
        List.Add(Ctrl)
    End Sub
End Class
